package org.linlinjava.litemall.db.service.distribution;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.DisWithdrawRecordMapper;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.util.WithdrawEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;


/**
 * 提现记录Dao
 *
 * @author xiaojiang
 * @Date 2018-05-30 16:55:08
 */
@Service
@Transactional
public class DisWithdrawRecordService{
    @Autowired
    private DisWithdrawRecordMapper withdrawRecordMapper;


    public List<DisWithdrawRecord> selectList(String userId, Integer page, Integer limit) {
        DisWithdrawRecordExample example = new DisWithdrawRecordExample();
        DisWithdrawRecordExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(userId)) {
            criteria.andDisUserIdEqualTo(userId);
        }
        example.orderBy(DisWithdrawRecord.Column.withdrawStatus.asc());
        PageHelper.startPage(page, limit);
        
        return withdrawRecordMapper.selectByExample(example);
    }

    public int countSeletive(String userId, Integer page, Integer limit, String sort) {
        DisWithdrawRecordExample example = new DisWithdrawRecordExample();
        DisWithdrawRecordExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(userId)) {
            criteria.andDisUserIdEqualTo(userId);
        }
        PageHelper.startPage(page, limit);

        return (int)withdrawRecordMapper.countByExample(example);
    }

    public void update(DisWithdrawRecord record) {
        withdrawRecordMapper.updateByPrimaryKeySelective(record);
    }

    public Integer countByUserId(Integer userId) {
        DisWithdrawRecordExample example = new  DisWithdrawRecordExample();
        DisWithdrawRecordExample.Criteria criteria = example.createCriteria();
        if (userId!=null) {
            criteria.andDisUserIdEqualTo(userId.toString());
        }
        return (int)withdrawRecordMapper.countByExample(example);
    }

    public void insert(DisWithdrawRecord withdrawRecord) {
        withdrawRecordMapper.insert(withdrawRecord);
    }

    public List<DisWithdrawRecord> selecByUserId(Integer userId, Integer page, String status) {
        DisWithdrawRecordExample example = new DisWithdrawRecordExample();
        DisWithdrawRecordExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(userId)) {
            criteria.andDisUserIdEqualTo(userId.toString());
        }
        if (!StringUtils.isEmpty(status)&&!"all".equals(status)) {
            criteria.andWithdrawStatusEqualTo(status);
        }
        example.orderBy(DisWithdrawRecord.Column.withdrawTime.desc());
        return withdrawRecordMapper.selectByExample(example);
    }

    public DisWithdrawRecord getById(Integer id) {
        return withdrawRecordMapper.selectByPrimaryKey(id);
    }
}
